Trò chơi game bắn súng đơn giản trong UNITY Engine
23.630 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class Pools : MonoBehaviour
5 {
6 //PREFABS
7 public GameObject prefabBonus;
8
9 public GameObject prefabBulletPlayer;
10 public GameObject prefabBulletEnemy;
11 public GameObject prefabEnemy_01;
12 public GameObject prefabEnemy_02;
13 public GameObject prefabEnemy_03;
14
15 //ARRAYS
16 GameObject[] bonuses;
17
18 GameObject[] enemies_01;
19 GameObject[] enemies_02;
20 GameObject[] enemies_03;
21 GameObject[] bulletsPlayer;
22 GameObject[] bulletsEnemy;
23
24 public GameObject bossSystem;
25
26 void Start()
27 {
28 enemies_01 = CreateObjects(prefabEnemy_01, 1);
29 enemies_02 = CreateObjects(prefabEnemy_02, 6);
30 enemies_03 = CreateObjects(prefabEnemy_03, 4);
31 bonuses = CreateObjects(prefabBonus, 8);
32 bulletsPlayer = CreateObjects(prefabBulletPlayer, 10);
33 bulletsEnemy = CreateObjects(prefabBulletEnemy, 700);
34 }
35
36 GameObject[] CreateObjects(GameObject prefab, int amount)
37 {
38 GameObject[] temp = new GameObject[amount];
39
40 for (int i = 0; i < amount; i++)
41 {
42 temp[i] = Instantiate(prefab);
43 temp[i].SetActive(false);
44 }
45
46 return temp;
47 }
48
49 //strings: p_bullet, e_bullet, enemy_01, enemy_02, enemy_03, bonus
50 public GameObject GetPoolableObject(string objName)
51 {
52 GameObject returningObj = null;
53 GameObject[] allObjects = null;
54
55 switch (objName)
56 {
57 case "p_bullet": allObjects = bulletsPlayer; break;
58 case "e_bullet": allObjects = bulletsEnemy; break;
59 case "enemy_01": allObjects = enemies_01; break;
60 case "enemy_02": allObjects = enemies_02; break;
61 case "enemy_03": allObjects = enemies_03; break;
62 case "bonus": allObjects = bonuses; break;
63 }
64
65 for (int i = 0; i < allObjects.Length; i++)
66 {
67 if (!allObjects[i].activeInHierarchy)
68 {
69 returningObj = allObjects[i];
70 }
71 }
72
73 return returningObj;
74 }
75
76 public void AcivateBoss()
77 {
78 bossSystem.SetActive(true);
79 bossSystem.transform.position = new Vector3(Camera.main.transform.position.x, 0, Camera.main.transform.position.z);
80 bossSystem.GetComponentInChildren<Enemy>().Activation();
81 }
82
83 //WHEN BOSS ACTIVATES
84 public void DeleteEnemiesAndBullets()
85 {
86 DeleteGroup(enemies_01);
87 DeleteGroup(enemies_02);
88 DeleteGroup(enemies_03);
89 DeleteGroup(bulletsEnemy);
90 }
91
92 void DeleteGroup(GameObject[] group)
93 {
94 for (int i = 0; i < group.Length; i++)
95 {
96 if (group[i].activeInHierarchy)
97 group[i].SetActive(false);
98 }
99 }
100 }
2 using System.Collections;
3
4 public class Pools : MonoBehaviour
5 {
6 //PREFABS
7 public GameObject prefabBonus;
8
9 public GameObject prefabBulletPlayer;
10 public GameObject prefabBulletEnemy;
11 public GameObject prefabEnemy_01;
12 public GameObject prefabEnemy_02;
13 public GameObject prefabEnemy_03;
14
15 //ARRAYS
16 GameObject[] bonuses;
17
18 GameObject[] enemies_01;
19 GameObject[] enemies_02;
20 GameObject[] enemies_03;
21 GameObject[] bulletsPlayer;
22 GameObject[] bulletsEnemy;
23
24 public GameObject bossSystem;
25
26 void Start()
27 {
28 enemies_01 = CreateObjects(prefabEnemy_01, 1);
29 enemies_02 = CreateObjects(prefabEnemy_02, 6);
30 enemies_03 = CreateObjects(prefabEnemy_03, 4);
31 bonuses = CreateObjects(prefabBonus, 8);
32 bulletsPlayer = CreateObjects(prefabBulletPlayer, 10);
33 bulletsEnemy = CreateObjects(prefabBulletEnemy, 700);
34 }
35
36 GameObject[] CreateObjects(GameObject prefab, int amount)
37 {
38 GameObject[] temp = new GameObject[amount];
39
40 for (int i = 0; i < amount; i++)
41 {
42 temp[i] = Instantiate(prefab);
43 temp[i].SetActive(false);
44 }
45
46 return temp;
47 }
48
49 //strings: p_bullet, e_bullet, enemy_01, enemy_02, enemy_03, bonus
50 public GameObject GetPoolableObject(string objName)
51 {
52 GameObject returningObj = null;
53 GameObject[] allObjects = null;
54
55 switch (objName)
56 {
57 case "p_bullet": allObjects = bulletsPlayer; break;
58 case "e_bullet": allObjects = bulletsEnemy; break;
59 case "enemy_01": allObjects = enemies_01; break;
60 case "enemy_02": allObjects = enemies_02; break;
61 case "enemy_03": allObjects = enemies_03; break;
62 case "bonus": allObjects = bonuses; break;
63 }
64
65 for (int i = 0; i < allObjects.Length; i++)
66 {
67 if (!allObjects[i].activeInHierarchy)
68 {
69 returningObj = allObjects[i];
70 }
71 }
72
73 return returningObj;
74 }
75
76 public void AcivateBoss()
77 {
78 bossSystem.SetActive(true);
79 bossSystem.transform.position = new Vector3(Camera.main.transform.position.x, 0, Camera.main.transform.position.z);
80 bossSystem.GetComponentInChildren<Enemy>().Activation();
81 }
82
83 //WHEN BOSS ACTIVATES
84 public void DeleteEnemiesAndBullets()
85 {
86 DeleteGroup(enemies_01);
87 DeleteGroup(enemies_02);
88 DeleteGroup(enemies_03);
89 DeleteGroup(bulletsEnemy);
90 }
91
92 void DeleteGroup(GameObject[] group)
93 {
94 for (int i = 0; i < group.Length; i++)
95 {
96 if (group[i].activeInHierarchy)
97 group[i].SetActive(false);
98 }
99 }
100 }
PREFABS
ARRAYS
strings: p_bullet, e_bullet, enemy_01, enemy_02, enemy_03, bonus
WHEN BOSS ACTIVATES